package com.stoneframe.controller.base;

import java.util.ArrayList;
import java.util.List;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stoneframe.service.BaseService;
import com.stoneframe.util.baseApp.AppHelper;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseApp.Pagination;
import com.stoneframe.util.baseApp.ToolHelper;

public abstract class BaseGenericController extends BaseController {
	
	public abstract BaseService getService();
	
	protected String className=this.getClass().getSimpleName()+".";
	
	/**
	 * 获取所有记录
	 */
	@RequestMapping(value="listAll")
	@ResponseBody
	public Object listAll(){
		logBefore(logger, className+"listAll");
		PageData pd=this.getPageData();
		List<PageData> list=new ArrayList<PageData>();
		try{
			list=getService().listAll(pd);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject(e.getMessage());
		}
		logAfter(logger);
		return AppHelper.successObject(list);
	}
	
	/**
	 * 分页获取记录
	 */
	@RequestMapping(value="listPage")
	@ResponseBody
	public Object listPage(Pagination page){
		logBefore(logger, className+"listPage");
		
		PageData pd=this.getPageData();
		page.setPd(pd);
		List<PageData> list=new ArrayList<PageData>();
		try{
			list=getService().listPage(page);
			PageData rePageData=new PageData();
			rePageData.put("rows", list);
			rePageData.put("total", page.getTotal());
			rePageData.put("page", page.getPage());
			rePageData.put("records", page.getRecords());
			
			logAfter(logger);
			return AppHelper.returnObject(pd, rePageData);
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject(e.getMessage());
		}
		
	}
	
	/**
	 * 获取详情
	 */
	@ResponseBody
	@RequestMapping(value="get")
	public Object get(){
		logBefore(logger,  className+"get");
		PageData pd=this.getPageData();
		try{
			pd=getService().findById(pd);
			return AppHelper.successObject(pd);
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return AppHelper.errorObject("获取失败；"+e.getMessage());
		}
	}
	
	/**
	 * 是否存在
	 */
	@ResponseBody
	@RequestMapping(value="isExist")
	public Object isExist(){
		logBefore(logger,  className+"isExist");
		PageData pd=this.getPageData();
		try{
			Boolean exist=getService().isExist(pd);
			return AppHelper.returnObject(pd, !exist);
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return AppHelper.errorObject("获取失败；"+e.getMessage());
		}
	}
	
	/**
	 * 更新记录
	 */
	@ResponseBody
	@RequestMapping(value="edit")
	public Object edit(){
		logBefore(logger, className+".edit");
		
		PageData pd=this.getPageData();
		
		try{
			if(ToolHelper.notEmpty(pd.getString("keyValue"))){
				getService().edit(pd);
			}else{
				pd.put("keyValue", this.get32Guid());
				getService().save(pd);
			}
			return AppHelper.successObject("操作成功！");
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject("操作失败！"+e.getMessage());
		}
	}
	
	/**
	 * 删除记录
	 */
	@ResponseBody
	@RequestMapping(value="del")
	public Object del(){
		logBefore(logger, className+"del");
		PageData pd=this.getPageData();
		try{
			getService().del(pd);
			return AppHelper.successObject("删除成功！");
		}catch(Exception e){
			logger.error(e.getMessage(), e);
			return AppHelper.errorObject("删除失败:"+e.getMessage());
		}
	}

}
